package com.taijihuabao.common.activititasklistener.reissuecardtasklistener;

import com.taijihuabao.modules.entity.ApprovalEntity;
import com.taijihuabao.modules.entity.ApprovalHistoryEntity;
import com.taijihuabao.modules.entity.DeptUserEntity;
import com.taijihuabao.modules.enums.optenums.checkin.ReissueCardEnum;
import com.taijihuabao.modules.enums.optenums.dept.DeptRoleEnum;
import com.taijihuabao.modules.mapper.ApprovalHistoryMapper;
import com.taijihuabao.modules.mapper.ApprovalMapper;
import com.taijihuabao.modules.mapper.DeptUserMapper;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 审批人监听
 *
 * @author Tu
 * @date 2021/12/24 16:11
 */
@Component
public class ReissueCardApprovalListener implements TaskListener {

    /**
     * 审批人任务节点名
     */
    private static final String APPROVAL_TASK_NAME = "审批人";

    @Resource
    private ApprovalMapper approvalMapper;
    @Resource
    private DeptUserMapper deptUserMapper;
    @Resource
    private ApprovalHistoryMapper approvalHistoryMapper;

    @Override
    public void notify(DelegateTask delegateTask) {
        String event = ReissueCardEnum.FirstEventName.getCode();
        //获取businessKey
        String businessKey = delegateTask.getExecution().getProcessInstanceBusinessKey();
        //获取补卡审批单
        ApprovalEntity approvalEntity = approvalMapper.selectById(Long.valueOf(businessKey));
        if (APPROVAL_TASK_NAME.equals(delegateTask.getName()) && event.equals(delegateTask.getEventName())) {
            //查询申请人id查询本部门审批人
            List<DeptUserEntity> applys = deptUserMapper.selectDeptByUserId(approvalEntity.getApplyUserId());
            String deptCode = applys.get(0).getDeptCode();
            List<DeptUserEntity> users = deptUserMapper.selectDeptByDeptCode(deptCode);
            //遍历部门人员查出审核员（设置一个）
            for (DeptUserEntity due : users) {
                if (DeptRoleEnum.Assessor.getCode().equals(due.getDeptRole())) {
                    //设置审核员
                    delegateTask.setAssignee(String.valueOf(due.getUserId()));
                    //修改审批任务
                    approvalEntity.setApprovalUserId(due.getUserId());
                    approvalEntity.setApprovalDeptCode(due.getDeptCode());
                    approvalEntity.setApprovalOpinion(null);
                    approvalEntity.setApprovalState("ing");
                    approvalEntity.setApprovalTask(delegateTask.getId());
                    approvalMapper.updateApproval(approvalEntity);
                    //审批历史数据
                    ApprovalHistoryEntity approvalHistoryEntity = new ApprovalHistoryEntity();
                    approvalHistoryEntity.setApprovalType(approvalEntity.getApprovalType());
                    approvalHistoryEntity.setApprovalCode(approvalEntity.getApprovalCode());
                    approvalHistoryEntity.setApplyUserId(approvalEntity.getApplyUserId());
                    approvalHistoryEntity.setApplyName(approvalEntity.getApplyName());
                    approvalHistoryEntity.setApplyDeptCode(approvalEntity.getApplyDeptCode());
                    approvalHistoryEntity.setApprovalUserId(due.getUserId());
                    approvalHistoryEntity.setApprovalDeptCode(due.getDeptCode());
                    approvalHistoryEntity.setApprovalState("ing");
                    approvalHistoryEntity.setApprovalTask(delegateTask.getId());
                    approvalHistoryEntity.setApprovalFlow(APPROVAL_TASK_NAME);
                    approvalHistoryMapper.insertApproval(approvalHistoryEntity);
                    break;
                }
            }
        }
    }

}
